﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using viNews.Classes;
using System.Data;
using visinia.SmartEngine.Cache;
using viNews.Providers;

namespace viNews.Controllers
{
    public class TagController
    {
        /// <summary>
        /// Gets all the tags
        /// </summary>
        /// <returns>List of all tags</returns>
        public static List<Tag> GetTags()
        {
            List<Tag> list = (List<Tag>)DataCache.GetCache("tags");
            if (list == null)
            {
                list = FillTags(viNewsDataProvider.Provider.GetTags());
                DataCache.SetCache("tags", list);
            }
            return list;
        }

        /// <summary>
        /// Gets tags for a specified news
        /// </summary>
        /// <param name="NewsId">News id</param>
        /// <returns>List of tags</returns>
        public static List<Tag> GetTags(int NewsId)
        {
            string key = string.Format("tags_{0}", NewsId);
            List<Tag> list = (List<Tag>)DataCache.GetCache(key);
            if (list == null)
            {
                list = FillTags(viNewsDataProvider.Provider.GetTags(NewsId));
                DataCache.SetCache(key, list);
            }
            return list;
        }

        /// <summary>
        /// Gets the tag by its name
        /// </summary>
        /// <param name="TagName"></param>
        /// <returns></returns>
        public static Tag GetTag(string TagName)
        {
            string TagNameLowered = TagName.Trim().ToLower();
            return GetTags().Find(p => p.Name.ToLower() == TagNameLowered);            
        }

        /// <summary>
        /// Gets the tag by its unique tag id
        /// </summary>
        /// <param name="tagid"></param>
        /// <returns></returns>
        public static Tag GetTag(int tagid)
        {
            return GetTags().Find(p => p.TagId == tagid);
        }

        #region DATA
        public static void ResetTags(int NewsId)
        {
            GetTags(NewsId).ForEach(delegate(Tag tag)
            {
                RemoveTag(tag.TagId, NewsId);
            });            
            DataCache.DeleteCacheMultiple("tags");
        }
        public static void InsertTag(int TagId, int NewsId)
        {            
            viNewsDataProvider.Provider.InsertTag(TagId, NewsId);
            DataCache.DeleteCacheMultiple("tags");
        }
        public static void RemoveTag(int TagId, int NewsId)
        {
            viNewsDataProvider.Provider.RemoveTag(TagId, NewsId);
            DataCache.DeleteCacheMultiple("tags");
        }

        public static int AddTag(Tag tag)
        {
            IDataReader dr = viNewsDataProvider.Provider.AddTag(tag.Name.Trim());
            int index = 0;
            while (dr.Read())
            {
                if (dr[0] != Convert.DBNull)
                    index = (int)dr[0];
            }
            dr.Close();
            DataCache.DeleteCacheMultiple("tags");
            return index;
        }

        public static void DeleteTag(Tag tag)
        {
            viNewsDataProvider.Provider.DeleteTag(tag.TagId);
            DataCache.DeleteCacheMultiple("tags");
        }
        #endregion

        #region HELPERS
        private static List<Tag> FillTags(IDataReader dr)
        {
            List<Tag> list = new List<Tag>();
            while (dr.Read())
            {
                list.Add(ActualRead(dr));
            }
            dr.Close();
            return list;
        }
        private static Tag ActualRead(IDataReader dr)
        {
            Tag tag = new Tag();
            if (dr["TagId"] != Convert.DBNull)
                tag.TagId = (int)dr["TagId"];
            if (dr["Name"] != Convert.DBNull)
                tag.Name = (string)dr["Name"];
            return tag;
        }
        #endregion
    }
}


